import { h, type FunctionalComponent } from 'vue';
import 'ant-design-vue/es/config-provider/style';
import { ConfigProvider } from 'ant-design-vue';
import dayjs from 'dayjs';
import 'dayjs/locale/zh-cn';
import zhCN from 'ant-design-vue/es/locale/zh_CN';

export const AConfigProvider: FunctionalComponent = (props: any, { slots }) => {
    const { locale, space, prefixCls, ...rest } = props;

    let localeData = locale;
    if (typeof locale === 'string') {
        if (locale.toLowerCase() === 'zh-cn') {
            dayjs.locale('zh-cn');
            localeData = zhCN;
        } else if (locale.toLowerCase() === 'en-us') {
            dayjs.locale('en');
            // 默认为英文
            localeData = undefined;
        }
    }

    const spaceData = { size: space };

    const prefixClsData = prefixCls || 'ant';

    return h(ConfigProvider, { locale: localeData, space: spaceData, prefixCls: prefixClsData, ...rest }, () => slots.default?.());
}